﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lidgren.Network;

namespace uam_fps_game.Network
{
    class Server
    {
        NetPeerConfiguration config;
        NetServer server;

        public Server()
        {
            config = new NetPeerConfiguration("Configure");
            config.Port = 14242;

            server = new NetServer(config);
            server.Start();
        }

        public void Update()
        {
            ManageIncomingMessages();
        }

        public void ManageIncomingMessages()
        {
            NetIncomingMessage msg;
            while ((msg = server.ReadMessage()) != null)
            {
                switch (msg.MessageType)
                {
                    case NetIncomingMessageType.Data:
                    case NetIncomingMessageType.VerboseDebugMessage:
                    case NetIncomingMessageType.DebugMessage:
                    case NetIncomingMessageType.WarningMessage:
                    case NetIncomingMessageType.ErrorMessage:
                        //Console.WriteLine(msg.ReadString());
                        break;
                    default:
                        //Console.WriteLine("Unhandled type: " + msg.MessageType);
                        break;
                }
                server.Recycle(msg);
            }
        }


        public void ServerStart()
        {
            server.Start();
        }

        public void ShutDown()
        {
            server.Shutdown("Bye bye");
        }

        public void SendMessage(string message, NetDeliveryMethod method)
        {
            NetOutgoingMessage sendMsg = server.CreateMessage();
            sendMsg.Write(message);
            server.SendMessage(sendMsg, server.Connections, method, 0);
        }
    }
}
